use std::error::Error;

use oauth2::http::StatusCode;
use thiserror::Error;


#[derive(Error, Debug)]
pub enum AuthError {
    #[error("env var not set: {0}")]
    NoEnvVar(&'static str),

    #[error("oauth2 url parse: {0}")]
    Oauth2Url(#[from] oauth2::url::ParseError),

    #[error("oauth2 url parse: {0}")]
    RequestTokenError(Box<dyn Error>/*#[from]  oauth2::RequestTokenError<
        oauth2::reqwest::Error<oauth2::reqwest::error::Error>, 
        oauth2::StandardErrorResponse<oauth2::reqwestBasicErrorResponseType>
    >*/),

    #[error("oauth2 configuration: {0}")]
    OauthConfig(#[from] oauth2::ConfigurationError),

    #[error("invalid header value: {0}")]
    InvalidHeaderValue(#[from] oauth2::http::header::InvalidHeaderValue),

    #[error("serde_json: {0}")]
    Json(#[from] serde_json::Error),

    #[error("introspection mode unknown: {0}")]
    InvalidIntrospectionApi(String),

    #[error("introspection: {0} {1}")]
    Introspection(StatusCode, String),
    
    #[error("pre-auth session not found")]
    PreAuthSessionNotFound,

    #[error("CSRF token mismatch")]
    CsrfTokenMismatch,

    #[error("Not logged in")]
    NotLoggedIn,


}


pub type Result<T, E=AuthError> = std::result::Result<T, E>;